#include <mylib/device/rgb_led.h>

#if PLATFORM_STM32
#    define RGB_LED_SETBIT(port, pin) HAL_GPIO_WritePin(port, pin, GPIO_PIN_SET)
#    define RGB_LED_CLEARBIT(port, pin) HAL_GPIO_WritePin(port, pin, GPIO_PIN_RESET)
#elif PLATFORM_MSPM0
#    define RGB_LED_SETBIT(port, pin) DL_GPIO_setPins(port, pin)
#    define RGB_LED_CLEARBIT(port, pin) DL_GPIO_clearPins(port, pin)
#endif

void rgb_led_set(rgb_led_t* led, bool r, bool g, bool b)
{
    if (r) {
        if (VALID_ON_LOW_LEVEL == led->valid) {
            RGB_LED_CLEARBIT(led->port_r, led->pin_r);
        }
        else {
            RGB_LED_SETBIT(led->port_r, led->pin_r);
        }
    }
    else {
        if (VALID_ON_LOW_LEVEL == led->valid) {
            RGB_LED_SETBIT(led->port_r, led->pin_r);
        }
        else {
            RGB_LED_CLEARBIT(led->port_r, led->pin_r);
        }
    }

    if (g) {
        if (VALID_ON_LOW_LEVEL == led->valid) {
            RGB_LED_CLEARBIT(led->port_g, led->pin_g);
        }
        else {
            RGB_LED_SETBIT(led->port_g, led->pin_g);
        }
    }
    else {
        if (VALID_ON_LOW_LEVEL == led->valid) {
            RGB_LED_SETBIT(led->port_g, led->pin_g);
        }
        else {
            RGB_LED_CLEARBIT(led->port_g, led->pin_g);
        }
    }

    if (b) {
        if (VALID_ON_LOW_LEVEL == led->valid) {
            RGB_LED_CLEARBIT(led->port_b, led->pin_b);
        }
        else {
            RGB_LED_SETBIT(led->port_b, led->pin_b);
        }
    }
    else {
        if (VALID_ON_LOW_LEVEL == led->valid) {
            RGB_LED_SETBIT(led->port_b, led->pin_b);
        }
        else {
            RGB_LED_CLEARBIT(led->port_b, led->pin_b);
        }
    }
}
